package com.baizhi.zjs.action;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import oracle.net.aso.k;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.struts2.ServletActionContext;
import org.omg.PortableInterceptor.USER_EXCEPTION;

import com.baizhi.zjs.entity.BookEntity;
import com.baizhi.zjs.entity.CategoryEntity;
import com.baizhi.zjs.service.BookService;
import com.baizhi.zjs.service.BookServiceImpl;
import com.baizhi.zjs.service.CategoryService;
import com.baizhi.zjs.service.CategoryServiceImpl;
import com.baizhi.zjs.util.UUIDUtil;
import com.opensymphony.xwork2.ActionSupport;
//图书的Action
public class BookACtion extends ActionSupport{
	//创建对象的成员
	private Integer  aoru;
	private List<CategoryEntity> list3;
	private BookEntity book;
	private BookEntity book2;
//获取上传文件
	private File file;
	//获取上传文件的文件名
	private String fileFileName;
	//
	private String saveFile;
	//-----book的list集合
	private List<BookEntity> list;
	
	//----id
	private String id;
	private String key;
	private String content;
	//---------------------------------------------------------
	public String addBook() throws Exception{
		//收参
		System.out.println(book);
		//-------------文件上传-------------
		String realPath = ServletActionContext.getServletContext().getRealPath(saveFile);
		FileUtils.copyFile(file, new File(realPath+"/"+fileFileName));
		book.setSrc(saveFile+"/"+fileFileName);
	//-----------------------------文件上传结束---------
		String uuid = UUIDUtil.getUuid();
		book.setId(uuid);
		book.setSales(0);
		book.setPutaeay(new Date());
		//System.out.println(book);
		BookServiceImpl impl = new BookServiceImpl();
		impl.addBook(book);
		//------------------------------
		//在图书分类中修改该分类中的图书数量
		//先查询该类别
		CategoryService impl2 = new CategoryServiceImpl();
		//创建一个对象来存储数据
		CategoryEntity entity = new CategoryEntity();
		entity.setCate_name(book.getDepartment());
		CategoryEntity entity2 = impl2.queryOneCate(entity);
		System.out.println("测试-------------------");
		//将类别中的数量加一
		System.out.println(entity2);
		System.out.println("添加图书数量修改测试----");
		//System.out.println(entity2.getCount());
		
		entity2.setCount(entity2.getCount()+1);
		System.out.println("修改数量");
		impl2.updateCDateCount(entity2);
		System.out.println(entity2.getCount());
	
		return "addBooks";
	}

	//查询所有的Action层

	public String showAllBook(){
		System.out.println("bookAction+=====================");
		BookService impl = new BookServiceImpl();
		list = impl.showAllBook();
		return "showAll";
	}
	
	
	//删除Action
	public String deleteBookAction(){
		System.out.println("删除的Action----------");
		BookService impl = new BookServiceImpl();
		System.out.println(id);
		//通过id 查询到该书籍的信息
		BookEntity entity = impl.shoeOneBook(id);
		/*获取该书籍的类别信息，并查询到该类别。*/
		CategoryServiceImpl impl2 = new CategoryServiceImpl();
		//创建一个类别对象，将book中类别存入类别对像中的类别名中
		CategoryEntity ca = new CategoryEntity();
		ca.setCate_name(entity.getDepartment());
		//获取该类别的详细信息
		CategoryEntity cate = impl2.queryOneCate(ca);
		//删除该信息，将获取到的类别对象cate中的count减一
		impl.deleteBook(id);
		//将count信息修改后，存入数据库
		cate.setCount(cate.getCount()-1);
		impl2.updateCDateCount(cate);
		//由于与添加结束后跳的页面相同所以用同一个字符串
		return "addBooks";
	}
	
	//查询一条图书信息,后台查询一条图书数据详情
	public String showOneBookAction(){
		BookService impl = new BookServiceImpl();
		book = impl.shoeOneBook(id);
		//创建session作用域将数据存入session中，然后再跳转到查询二级类别的Action中
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.setAttribute("book", book);
		return "showOneBook";
	}
	
	//修改图书信息的Action层
	public String updateBookAction() throws Exception{
		System.out.println("修改的Action=============");
		//-------------文件上传-------------
		if(file !=null){
			String realPath = ServletActionContext.getServletContext().getRealPath(saveFile);
			FileUtils.copyFile(file, new File(realPath+"/"+fileFileName));
			book.setSrc(saveFile+"/"+fileFileName);
		}
			
			//-----------------------------文件上传结束---------
		System.out.println(book);
		//-------------------------修改类别中的count数量-------------
		//1,从session中获取需要修改的对象的id，对该数据进行查询命名为对象1。
		//2,,判断，查询的对象1与传回来的修改的后对象信息中的所属分类是否相同，
		// * 如果不相同进行修改，如果相同就不用动
		//1）,不相同，创建一个类别的对象cate1，对象1中的所属类别放进创建对象cate1的属性：类别名 中
		//2）通过这个类别对象cate1用条件查询去查询详细的类别信息，并获取count,将count-1
		
		//3),创建一个对象cate2,将传回来的要修改的book对象中的所属类别存入cate2中的类别名中
		//4)通过这个类别对象cate1用条件查询去查询详细的类别信息，并获取count,将count+1
		//------------------------------
		BookServiceImpl impl = new BookServiceImpl();
		impl.updateOneBook(book);
		//由于与添加,删除结束后跳的页面相同所以用同一个字符串
		
		return "addBooks";
	}
	
	//模糊查询
	public String likeBookAction(){
		System.out.println("模糊查询-------");
		System.out.println(key +"   " +content);
		//
		BookEntity book3 = new BookEntity();
		
		//判断收到的参数
		if("作者".equals(key)) book3.setWritwer(content);
		if("书名".equals(key)) book3.setBookname(content);
		if("出版社".equals(key)) book3.setPress(content);
		System.out.println(book3);
		BookServiceImpl impl = new BookServiceImpl();
		list = impl.likeBook(book3);
		return "likeBook";
	}
	
	//查询二级类别
	public String showSecondLevelAction(){
		//判断是修改调用该Action还是添加调用

		CategoryServiceImpl impl = new CategoryServiceImpl();
		list3 = impl.showSecondLevel();
		System.out.println(aoru);
		if(aoru==null){
			return "upbook";
		
		}else{
			return "addbook";
		}
		
	
	}
	
	
	
	
	
	
	
	
	//---------------------------------------------------------
	public BookEntity getBook() {
		return book;
	}

	public void setBook(BookEntity book) {
		this.book = book;
	}




	public File getFile() {
		return file;
	}




	public void setFile(File file) {
		this.file = file;
	}




	public String getFileFileName() {
		return fileFileName;
	}




	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}




	public String getSaveFile() {
		return saveFile;
	}




	public void setSaveFile(String saveFile) {
		this.saveFile = saveFile;
	}

	public List<BookEntity> getList() {
		return list;
	}

	public void setList(List<BookEntity> list) {
		this.list = list;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public BookEntity getBook2() {
		return book2;
	}

	public void setBook2(BookEntity book2) {
		this.book2 = book2;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Integer getAoru() {
		return aoru;
	}

	public void setAoru(Integer aoru) {
		this.aoru = aoru;
	}

	public List<CategoryEntity> getList3() {
		return list3;
	}

	public void setList3(List<CategoryEntity> list3) {
		this.list3 = list3;
	}





	
}
